package com.indianindie;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.util.Log;

public class SRTextureComponent extends SRGameComponent  {
	
	private final int TOTAL_NUM_TEXTURES;
	
	public int[] textures = new int[this.TOTAL_NUM_TEXTURES];

	private FloatBuffer vertexBuffer;

	private FloatBuffer textureBuffer;

	private ByteBuffer indexBuffer;
	
	public static final int BACKGROUND = 0;
	
	private float vertices[] = {
               0.0f, 0.0f, 0.0f, 
               1.0f, 0.0f, 0.0f,  
               0.05f, 0.05f, 0.0f,  
               0.0f, 1.0f, 0.0f,
                             };

	private float texture[] = {         
               0.0f, 0.0f,
               1.0f, 0f,
               1f, 1.0f,
               0f, 1f, 
                              };
    
	private byte indices[] = {
               0,1,2, 
               0,2,3, 
                              };
	
	
	public SRTextureComponent(GL10 gl, int textureCount) {
		this.TOTAL_NUM_TEXTURES = textureCount;
		textures = new int[this.TOTAL_NUM_TEXTURES];
		gl.glGenTextures(textureCount, textures,0);
		
	}
	
	
	public void init(GL10 gl) {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		
		  ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
		  byteBuf.order(ByteOrder.nativeOrder());
		  vertexBuffer = byteBuf.asFloatBuffer();
		  vertexBuffer.put(vertices);
		  vertexBuffer.position(0);
			
		  byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
		  byteBuf.order(ByteOrder.nativeOrder());
		  textureBuffer = byteBuf.asFloatBuffer();
		  textureBuffer.put(texture);
		  textureBuffer.position(0);
			
		  indexBuffer = ByteBuffer.allocateDirect(indices.length);
		  indexBuffer.put(indices);
		  indexBuffer.position(0);
	}

	
	public void draw(GL10 gl,SRGameDisplayObject gameObj) {
		
	  
	  Log.i("draw","test");	
	  
	
	  //gl.glPushMatrix();
	  
	  //gameObj.setUpOpenGL(gl);
	
	  
	  //gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[gameObj.getTexture_id()]);
	  
	  gl.glFrontFace(GL10.GL_CCW);
	  //gl.glEnable(GL10.GL_CULL_FACE);
	  //gl.glCullFace(GL10.GL_BACK);
	       
	  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	  gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	
	  gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
	  gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
	  
	  gl.glColor4f(0.0f, 0.5f, 0.0f, 0.0f);
	  
	  gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);      
	  
	  gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	  gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	  gl.glDisable(GL10.GL_CULL_FACE);
	  
	  //gl.glPopMatrix();
	}

	
    public void loadTexture(GL10 gl, Context context, SRGameDisplayObject gameObj) {
    		
    	  
	      InputStream imagestream = context.getResources().openRawResource(gameObj.getTexture_resource());
	      Bitmap bitmap = null;
	      try {
	        
	    	  bitmap = BitmapFactory.decodeStream(imagestream);

	      }catch(Exception e){
	    	  
	      }finally {
	         //Always clear and close
	         try {
	        	 imagestream.close();
	        	 imagestream = null;
	         } catch (IOException e) {
	         }
	      }

	      gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[gameObj.getTexture_id()]);
	      
	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

	      if(gameObj.repeatTexture()) {
	    	  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
	    	  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
	      }
	      else {
		      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
		      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
	      }
	      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
	      
	      bitmap.recycle();
	   }
    
   
}
